<?php
/*
 require "../maths/tool_maths.php";
 require "../array/tool_array.php";
*/

 class tool_alignement{

  //DEPRECATED
  //NEED CLEAN_CHECK -> TOOL_MATHS::GET_PERCENT_VECTOR()
  public static function array_percent($_array){
    return tool_maths::get_percent_vector($_array);
  }

  public static function fusion_sum($_array,$_groupe){
    $sum=0;
    foreach($_groupe as $i) $sum+=$_array[$i];
    return $sum;
  }

  public static function align_aux($_array1,$_array2){
    $array_group_equiv=array();
    $array_group_equiv[]=array(array(0),array(0));
    $cpt=1;
    for($i=1,$j=1;$i<count($_array1)-1 and $j<count($_array2)-1;){
      $last_group=end($array_group_equiv); 
      if(!isset($current_group)){
        $current_group=array(array($i),array($j));
      }

      $last_sum1=self::fusion_sum($_array1,$last_group[0]);
      $last_sum2=self::fusion_sum($_array2,$last_group[1]);

      $current_sum1=self::fusion_sum($_array1,$current_group[0]);
      $current_sum2=self::fusion_sum($_array2,$current_group[1]);
      $vit1 = $current_sum1-$last_sum1;
      $vit2 = $current_sum2-$last_sum2;

      if(tool_maths::match_value2($vit1,$vit2,2)){
        $array_group_equiv[]=$current_group;
        ++$cpt; ++$j; ++$i;
        unset($current_group);
      }
      elseif($vit1 < $vit2) $current_group[0][]=++$i;
      else $current_group[1][]=++$j; 
    }

    if(!isset($current_group)){
      $current_group=array();
    }

    for($i;$i<count($_array1);++$i){
      $current_group[0][]=$i;
    }
    for($j;$j<count($_array2);++$j){
      $current_group[1][]=$j;
    }

    if(isset($current_group[0]) and isset($current_group[1])){
      $array_group_equiv[]=$current_group;
    }

    else{
      if(isset($current_group[0])){
        foreach($current_group[0] as $elt){
          if(!in_array($elt,$array_group_equiv[$cpt-1][0]))
            $array_group_equiv[$cpt-1][0][]=$elt;
        }
      }

      elseif(isset($current_group[1])){
        foreach($current_group[1] as $elt){
          if(!in_array($elt,$array_group_equiv[$cpt-1][1]))
            $array_group_equiv[$cpt-1][1][]=$elt;
        }
      }
    }

    return $array_group_equiv;

  }

  public static function align_repartition($_array1,$_array2){
    $_array1 = tool_array::array_merge(array(0),$_array1);
    $_array2 = tool_array::array_merge(array(0),$_array2);
    $_array1 = tool_maths::get_percent_vector($_array1);
    $_array2 = tool_maths::get_percent_vector($_array2);
    $array_group_equiv = self::align_aux($_array1,$_array2);
    return $array_group_equiv;
  }

  public static function align_structure($_struct1,$_struct2){
    $rep1=tool_tree::structure2strlen($_struct1);
    $rep2=tool_tree::structure2strlen($_struct2);
    $align=self::align_repartition($rep1,$rep2);
    return $align;
  }

  public static function array_group_equiv_simplified(&$_array_group_equiv){
    $array_res=array();
    foreach($_array_group_equiv as $id_groupe=>$groupe){
      if(count($groupe[0])>1 or count($groupe[1])>1){
        $array_res[$id_groupe]=$groupe;
      }
    }
    return $array_res;
  }

  public static function array_group_equiv_clean($_array_group_equiv){
    $array_res=array();
    foreach($_array_group_equiv as $id_groupe=>$groupe){
      $array_res[$id_groupe]=$groupe;
      foreach($groupe as $id_doc=>$array_seg){
        foreach($array_seg as $i=>$id_seg){
          $array_res[$id_groupe][$id_doc][$i]=$id_seg-1;
        }
      }
    }
    return $array_res;
  }

  public static function repartition2graph($_rep,$_title,$_max=null){
    $apente=tool_maths::get_array_pente($_rep);
    $tool_graph=new Tool_pear_graph(600,200);
    ob_start();
    $tool_graph->__add_title($_title);
    if($_max!=null) $tool_graph->__set_max_x($_max);
    //fill_graph_aux($tool_graph,array_keys($apente),array_values($apente));
    $tool_graph->__add_plot_title("évolution nb. caractères / segment");
    $tool_graph->__add_array_x(array_keys($apente));
    $tool_graph->__add_array_y(array_values($apente));
    $tool_graph->__add_plot_title("nb. caractères / segment");
    $tool_graph->__add_array_x(array_keys($_rep));
    $tool_graph->__add_array_y(array_values($_rep));
    $graph=$tool_graph->plot05();
    //$graph=fill_graph($tool_graph,array_keys($rep),array_values($rep));
    $graph->done();
    $out_svg=ob_get_contents();
    ob_clean();
    return mb_ereg_replace("<\?[^?]+\?>\n<\![^>]+>\n","",$out_svg);
  }  

  public static function all_equiv2one_equiv($_all_equiv,$id){
    $res=array();
    foreach($_all_equiv as $id_equiv=>$group){
      $res[$id_equiv]=$group[$id];
    }
    return $res;
  }

  public static function reverse_equiv($_equiv){
    $res=array();
    foreach($_equiv as $id_group=>$group){
      foreach($group as $id_segment){
        $res[$id_segment]=$id_group;
      }
    }
    return $res;
  }

  public static function equiv2graph($_rep1,$_rep2,$_max=null,$_max_y=1000){
    $equiv=self::align_repartition($_rep1,$_rep2);
    $equiv0=self::all_equiv2one_equiv($equiv,0);
    $equiv1=self::all_equiv2one_equiv($equiv,1);
    $rev_equiv0=self::reverse_equiv($equiv0);
    $rev_equiv1=self::reverse_equiv($equiv1);
//    print_r($equiv);
//    die();
    $_rep1=tool_maths::do_translation($_rep1,1,0);
    $_rep2=tool_maths::do_translation($_rep2,1,0);
//    $_array_group_equiv=tool_alignement::array_group_equiv_clean($equiv);
//    $equiv=array_slice($equiv,1);
//    $_array_group_equiv=tool_array::array_merge(array(),$equiv);
    $_array_group_equiv=$equiv;
    $tool_graph=new Tool_pear_graph(600,300);
    if($_max!=null) $tool_graph->__set_max_x($_max);
    $tool_graph->__set_min_x(0);
//    print_r($_array_group_equiv);
//    die();
    $tool_graph->__add_array_x(array_keys($_rep1));
    $tool_graph->__add_array_y(array_values($_rep1));
    $_rep2=tool_array::array_neg($_rep2);
    $tool_graph->__add_array_x(array_keys($_rep2));
    $tool_graph->__add_array_y(array_values($_rep2));
    array_shift($_array_group_equiv);
    foreach($_array_group_equiv as $id_group=>$group){
//      if($id_group>0){
        $tool_graph->__add_plot_title("groupe $id_group");
        $f0=reset($group[0])-0.2;
        if($f0<0) $f0=0;
        $l0=end($group[0])+0.2;
        $f1=reset($group[1])-0.2;
        if($f1<0) $f1=0;
        $l1=end($group[1])+0.2;
        $array_x=array($f0,$f0,$l0,$l0,$l1,$l1,$f1,$f1);
        $array_y=array(0,$_max_y/2,$_max_y/2,0,0,-$_max_y/2,-$_max_y/2,0);
        $tool_graph->__add_array_x($array_x);
        $tool_graph->__add_array_y($array_y);
//      }
    }
    $graph=$tool_graph->plot06();
    ob_start();
    $graph->done();
    $out_svg=ob_get_contents();
    ob_clean();
    return array(
      "equiv"=>$equiv,
      "rev_equiv0"=>$rev_equiv0,
      "rev_equiv1"=>$rev_equiv1,
      "graph"=>mb_ereg_replace("<\?[^?]+\?>\n<\![^>]+>\n","",$out_svg)
    );
  }

  //
  //méthode de groupage de structure endogène au document
  // 

  public static function group_structure($_rep1,$_rep2,$_equiv){
   array_shift($_equiv);
   $rep=array($_rep1,$_rep2);
   $array_rep=array();
   foreach($_equiv as $array_group){
     foreach($array_group as $id_group=>$group){
       if(count($group)==1){
         $id_seg=$group[0]-1;
         $array_rep[$id_group][]=$rep[$id_group][$id_seg];
       }
       else{
         $bloc=new Tree_xhtml(new tag_div());
         foreach($group as $id_seg){
           $id_seg=$id_seg-1;
           $tree=$rep[$id_group][$id_seg];
           $sons=$tree->__get_array_tree();
           $bloc->__add_array_tree($sons);
         }
         $array_rep[$id_group][]=$bloc;
       }
     }
   }

   $document_xml=new document_xml(); 
   $struct0=$document_xml->__get_structure($array_rep[0]);
   $struct1=$document_xml->__get_structure($array_rep[1]);
   return array($struct0,$struct1);
  }

  public static function group_structure2($_rep1,$_rep2,$_equiv){
   array_shift($_equiv);
   $rep=array($_rep1,$_rep2);
   $array_rep=array();
   foreach($_equiv as $array_group){
     foreach($array_group as $id_group=>$group){
       $bloc=new Tree_xhtml(new tag_div());
       foreach($group as $id_seg){
         $id_seg=$id_seg-1;
         $tree=$rep[$id_group][$id_seg];
         $sons=$tree->__get_array_tree();
         $bloc->__add_array_tree($sons);
       }
       $array_rep[$id_group][]=$bloc;
     }
   }
   
   $document_xml=new document_xml(); 
   $struct1=$document_xml->__get_structure($array_rep[1]);
   $sign1=$struct1->__get_signature();
   $struct0=tool_tree::structure_segment2structure($sign1,$array_rep[1]);
   return array($struct0,$struct1);
  }
}

//$array1=array(32,143,537,202,1256,62,694,0,71);
//$array2=array(27,124,733,302,1225,308,419,69,1055,0,71);

//$array1=array(10,30,30,10);
//$array2=array(40,40);

//$array1=array(0,10,80,0);
//$array2=array(0,10,40,40,0);

//$array1=array(0,50,50,0);
//$array2=array(0,20,30,10,10,10,10,10,0);

//$array1_percent=array_percent($array1);
//$array2_percent=array_percent($array2);

//align($array1_percent,$array2_percent);
//print_r(align($array1_percent,$array2_percent));
//print_r($array1_percent);
//print_r($array2_percent);
/*
$res=tool_alignement::align_repartition($array1,$array2);
$res_simp=tool_alignement::array_group_equiv_simplified($res);
$xhtml=tool_alignement::align2xhtml(null,null,$res_simp);
print_r($xhtml);
*/
?>
